<?php
/**
 * Trieda na automaticke nacitanie jazyka
 * @author Tomas Srna
 * @package bakalarka
 */

class L
{
	private static $langInstances = array();
	private static $lang = NULL;
	
	public static function __callStatic($name, $arguments)
	{
		$l = NULL;
		
		if($name == 'selected')
		{
			if(self::$lang)
				$l = self::$lang;
			else if(array_key_exists('lang', $_COOKIE))
				$l = $_COOKIE['lang'];
			else
				$l = Config::$languageDefault;
		}
		else
			$l = $name;
			
		if(array_key_exists($l, self::$langInstances))
			return self::$langInstances[$l];
		
		$lc = ucfirst($l).'Language';
		self::$langInstances[$l] = new $lc();
		
		return self::$langInstances[$l];
	}
	
	public static function translate($str, $language = 'selected')
	{
		if(substr($str, 0, 3) != "L::")
			return $str;
		else
			$str = substr($str, 3);
		
		$params = explode(';', $str, 2);
		$c = count($params);
		if($c > 0)
		{
			$string = L::$language()->{$params[0]};
		}
		if($c > 1)
		{
			$string = vsprintf($string,	json_decode($params[1]));
		}
		
		return F::he($string);
	}
}
